package com.yylc.cscore.aop;

import com.yylc.cscore.util.ResultUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author xiangnan
 * date 2018/4/27
 */
@Aspect
@Component
@Order(1)
public class ResultAspect {
    private static final Logger logger = LogManager.getLogger();

    @Around("execution(public * com.yylc.cscore.controller.CustomerController.*(..)) ||" +
            "execution(public * com.yylc.cscore.controller.TaskRuleController.*(..)) ||" +
            "execution(public * com.yylc.cscore.controller.TaskController.*(..)) ||" +
            "execution(public * com.yylc.cscore.controller.TaskTypeController.*(..)) ||" +
            "execution(public * com.yylc.cscore.controller.UserController.*(..)) ||" +
            "execution(public * com.yylc.cscore.controller.LoginController.*(..))")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            Object result = joinPoint.proceed();
            return ResultUtil.success(result);
        } catch (Exception e) {
            logger.error("invoke error: {} {}, e={}", joinPoint.getSignature(), joinPoint.getArgs(), e);

            // 返回异常信息
            return ResultUtil.error(e.getMessage());
        }
    }

}
